package com.epalmpay.controller.channel.system;

import java.util.Date;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.epalmpay.commom.BaseController;
import com.epalmpay.status.Rescode;
import com.epalmpay.dto.systemparamssetting.SystemParamsSettingInDTO;
import com.epalmpay.dto.systemparamssetting.SystemParamsSettingOutDTO;
import com.epalmpay.entity.GroupConfig;
import com.epalmpay.service.common.impl.SystemParamsSettingImpl;
import com.epalmpay.util.PropertyCopyUtil;

/**
 * Created by zf on 2017/5/4.
 */
@Controller
@RequestMapping(value="/merchant/system")
public class SystemParamsSettingController  extends BaseController{
    @Resource
    private SystemParamsSettingImpl systemParamsSettingService;
    //
    /**
     * 用户名密码验证
     * 验证成功返回用户信息
     */
    @RequestMapping(value="/UpdateSystemParams")
    @ResponseBody
    public SystemParamsSettingOutDTO UpdateSystemParams(SystemParamsSettingOutDTO request){

        SystemParamsSettingOutDTO systemParamsSettingOut=new SystemParamsSettingOutDTO();

        try {
            GroupConfig groupConfig=new GroupConfig();
            PropertyCopyUtil.copyPropertiesExclude(request,groupConfig);
            Date date=new Date();

            groupConfig.setGmtModified(date );
            int status=systemParamsSettingService.updateSystemParamsByGroupId(groupConfig);


            PropertyCopyUtil.copyPropertiesExclude(groupConfig,systemParamsSettingOut);

            //对象反射复制
            if(status>0){
                systemParamsSettingService.updateGroupProfit(groupConfig);


                systemParamsSettingOut.setErrorCode(Rescode.SUCCESS, "处理成功");
            } else{
                systemParamsSettingOut.setErrorCode(Rescode.FAIL, "处理出现异常");
            }
        } catch (Exception e) {
            systemParamsSettingOut.setErrorCode(Rescode.FAIL, "处理出现异常");
            e.printStackTrace();
        }
        return systemParamsSettingOut;
    }

    /**
     * 用户名密码验证
     * 验证成功返回用户信息
     */
    @RequestMapping(value="/findByGroupId")
    @ResponseBody
    public SystemParamsSettingOutDTO querySystemParams(SystemParamsSettingInDTO request){
        SystemParamsSettingOutDTO systemParamsSettingOut=new SystemParamsSettingOutDTO();
        try {


            GroupConfig systemInfo=systemParamsSettingService.getSystemParamsByGroupId(request.getGroup_id());
            //对象反射复制
            if(systemInfo!=null){

                PropertyCopyUtil.copyPropertiesExclude(systemInfo,systemParamsSettingOut);
                systemParamsSettingOut.setErrorCode(Rescode.SUCCESS, "处理成功");
            }else if(systemInfo==null){
                GroupConfig groupConfig=new GroupConfig();
                groupConfig.setGroupId(request.getGroup_id());
                Date date=new Date();
                groupConfig.setGmtCreate(date);
//                要不要判断插入数据是否成功
                int status=systemParamsSettingService.insetSystemParamsByGroupId(groupConfig);

                PropertyCopyUtil.copyPropertiesExclude(groupConfig,systemParamsSettingOut);

                systemParamsSettingOut.setErrorCode(Rescode.SUCCESS, "处理成功");

            } else{
                systemParamsSettingOut.setErrorCode(Rescode.FAIL, "处理出现异常");
            }
        } catch (Exception e) {
            systemParamsSettingOut.setErrorCode(Rescode.FAIL, "处理出现异常");
            e.printStackTrace();
        }
        return systemParamsSettingOut;
    }



    /**
     * 设置系统参数
     * 验证成功返回用户信息
     */
    @RequestMapping(value="/SetSystemParams")
    @ResponseBody
    public SystemParamsSettingOutDTO toSetSystemParams(@RequestBody SystemParamsSettingOutDTO request){
        SystemParamsSettingOutDTO systemParamsSettingOut=new SystemParamsSettingOutDTO();
        try {

            GroupConfig groupConfig=new GroupConfig();
            PropertyCopyUtil.copyPropertiesExclude(request,groupConfig);
            Date date=new Date();
            groupConfig.setGmtCreate(date);
            groupConfig.setGmtModified(date );
            //groupConfig.setValidTime(date);
            int status=systemParamsSettingService.insetSystemParamsByGroupId(groupConfig);

            PropertyCopyUtil.copyPropertiesExclude(groupConfig,systemParamsSettingOut);

            //对象反射复制
            if(status>0){
                systemParamsSettingOut.setErrorCode(Rescode.SUCCESS, "处理成功");
            }else{
                systemParamsSettingOut.setErrorCode(Rescode.FAIL, "处理出现异常");
            }
        } catch (Exception e) {
            systemParamsSettingOut.setErrorCode(Rescode.FAIL, "处理出现异常");
            e.printStackTrace();
        }
        return systemParamsSettingOut;
    }

}
